<?php
namespace jy_weishop\page;
defined('BY_JYA') or exit('error');
/*
	页面返回
*/
class goods_index extends \plugin{
	public function getmain($return=array()){
		global $_RQ;
		//下面list,PC端需要获取商品列表
		$params=array(
			'uniacid'=>$_SESSION['uniacid'],
			'status'=>1,
			'nochecked'=>0,
			'no_showsys'=>0,
			'type <>'=>4
		);
		$searchkeyword=$this->plugin_setting('search')['keyword'];
		$return['searchkeyword']=explode(';',$searchkeyword);
		if($_RQ['cid']){
			$params['ccate']=intval($_RQ['cid']);
			$return['params']['ccate']=intval($_RQ['cid']);
		}
		
		if(trim($_RQ['keyword'])){
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['keyword']=trim($_RQ['keyword']);
		}
		if($_RQ['pid']){
			$params['pcate']=intval($_RQ['pid']);
			$return['params']['pid']=intval($_RQ['pid']);
		}else{
			$return['params']['pid']=0;
		}
		if($_RQ['brandid']){
			$return['params']['brandid']=$_RQ['brandid'];
		}
		if($_RQ['order']=='isrecommand'){
			$params['saletype likes']=array('1');
			$return['params']['order']='isrecommand';
		}
		if($_RQ['order']=='isnew'){
			$params['saletype likes']=array('2');
			$return['params']['order']='isnew';
		}
		if($_RQ['order']=='ishot'){
			$params['saletype likes']=array('3');
			$return['params']['order']='ishot';
		}
		if($_RQ['priceMin']){
			$params['price >=']=intval($_RQ['priceMin']);
			$return['params']['priceMin']=intval($_RQ['priceMin']);
		}
		if($_RQ['priceMax']){
			$params['price <=']=intval($_RQ['priceMax']);
			$return['params']['priceMax']=intval($_RQ['priceMax']);
		}
		if(trim($_RQ['pluginname'])){
			$params['pluginname']=trim($_RQ['pluginname']);
			$return['params']['pluginname']=trim($_RQ['pluginname']);
		}
		$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
		if($onepluginname){//开启独立商品
				$params['onepluginname']=$onepluginname;
				$return['params']['onepluginname']=$onepluginname;
		}else{
				$params['onepluginname']='';
		}
		//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
		$pindex = max(1, intval($_RQ['page']));
		$psize = 20;
		if($_RQ['num']){
			$psize=$_RQ['num'];
		}
		$total='';
		$fields=array('id','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit');
		$orderby='paixu desc, star desc, id desc ';
		if($_RQ['order']){
			if($_RQ['order']=='sales'){
				$orderby='salerealnum desc';
			}
			if($_RQ['order']=='price'){
				if($_RQ['order_type']==2){
					$orderby='price desc';
				}else{
					$orderby='price asc';
				}
				
			}
		}
		$parameter=array(
			'params'=>$params,
			'limit'=>array($pindex,$psize),
			'fields'=>$fields,
			'orderby'=>$orderby,
		);
		//PC端先关闭获取，教给JS
		if($_SESSION['os']=='windows' && false){
			if($_RQ['order']=='sale_offer'){
				$list=model('jy_weishop/activity/sales')->getoffer();
			}else{
				$list=$this->model('goods')->goods_list($parameter,$total);
			}
		}
		$return['list']=array();
		$return['order']=$_RQ['order'];
		//最近浏览
		$params=array(
			//'table'=>'jy_weishop_member_history',
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),
			'fields'=>array('id','goodsid','merchid','createtime'),
			'orderby'=>'createtime desc',
			'limit'=>'5',
		);
		if($_SESSION['os']=='windows'){
			$hlist=pdo_getlist('jy_weishop_member_history',$params);
			$return['brands']=pdo_getall('jy_weishop_goods_brand',array('image <>'=>'','uniacid'=>$_SESSION['uniacid'],'isshow'=>1),'','','paixu desc,id desc');
		}
		if($hlist){
			foreach($hlist as &$row){
				$goods=$this->model('goods')->goods_detail($row['goodsid'],array('title','image','price'));
				$row=array_merge($goods,$row);
			}
		}
		$return['hlist']=$hlist;
		$groupparams=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0);
		if($onepluginname){//开启独立商品
				$groupparams['onepluginname']=$onepluginname;
		}else{
				$groupparams['onepluginname']='';
		}
		$return['member']=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>0),array('mid','level'));
		$levelinfo=$levelinfo['level'];//会员等级信息
		$groupparams['showlevels OR']='ifnull(showlevels,\'\')=\'\' or showlevels like \'%;'.trim($levelinfo).';%\'';//等级浏览权限
		$category=pdo_getall('jy_weishop_goods_group',$groupparams,array(),'','paixu desc');
		foreach($category as &$item){
			$item['childs']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$item['id']),array(),'','paixu desc');
			if($_RQ['pid'] && $_RQ['pid']==$item['id']){
				$return['childs']=$item['childs'];
			}
		}
		//推荐分类
		$groupparams['isrecommand']=1;
		$recommend=pdo_getall('jy_weishop_goods_group',$groupparams,array(),'','paixu desc');
		$return['recommend']=$recommend;
		$return['category']=$category;
		$title=$return['setting']['share_title']?'全部商品'.$return['setting']['share_title']:'全部商品-在线商城';
		$desc=$return['setting']['share_desc']?'商品列表-'.$return['setting']['share_desc']:'在线商城，一个属于自己的个人商城！';
		if($_RQ['pcate'] || $_RQ['pid']){
			$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
			$return['params']['pid']=$pcate;
			$pcateinfo=pdo_get('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pcate));
			$title=$pcateinfo['title'];
			$desc=$pcateinfo['desc'];
			$return['ptitle']=$title;
		}
		if($_RQ['ccate'] || $_RQ['cid']){
			$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
			$return['params']['ccate']=$ccate;
			$ccateinfo=pdo_get('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ccate));
			//print_r($ccateinfo);exit;
			$title=$ccateinfo['title'];
			$desc=$ccateinfo['desc'];
			$return['ctitle']=$title;
			$pcateinfo=pdo_get('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ccateinfo['parentid']));
			$return['params']['pid']=$ccateinfo['parentid'];
			$return['ptitle']=$pcateinfo['title'];
		}elseif($_RQ['order']=='isrecommand'){
			$title='推荐商品';
		}elseif($_RQ['order']=='isnew'){
			$title='新品上市';
		}elseif($_RQ['order']=='ishot'){
			$title='热卖商品';
		}elseif($_RQ['order']=='issendfree'){
			$title='包邮商品';
		}
		if($_SESSION['os']=='windows' && $return['params']['pid']){
			$return['childs']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$return['params']['pid']),array(),'','paixu desc');
		}
		$return['sysparams']=pdo_getall('jy_weishop_goods_param',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'is_so'=>1));
		foreach($return['sysparams'] as &$pv){
			$pv['content']=json_decode($pv['content'],true);
		}
		$return['labels']=pdo_getall('jy_weishop_goods_label',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title','labelname'));
		foreach($return['labels'] as &$labv){
			$labv['list']=json_decode($labv['labelname'],true);
		}
		$return['catlevel']=2;
		$return['opencategory']=true;
		$return['feemoney']=$this->plugin_setting('feemoney');
		$return['sale_tan']=model('jy_weishop/activity/sales')->gettan(1);
		$return['grouptitle']=$title;
		$return['sharedata']=array(
			'title'=>$title.'-'.$return['shopset']['title'],
			'desc'=>$desc,
			'link'=>custom_url('jy_weishop/goods.index//fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>ATTACHMENT_URL.$return['setting']['share_image']
		);
		return $return;
	}
}